#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Controls;
using System.Text.RegularExpressions;
using System.Diagnostics;

#endregion

namespace VisualDB
{
    public partial class Form1 : Form
    {
        private List<string> keywords = new List<string>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            keywords.Clear();
            keywords.Add("select");
            keywords.Add("insert");
            keywords.Add("update");
            keywords.Add("delete");
            keywords.Add("from");
            keywords.Add("where");
            keywords.Add("order");
            keywords.Add("by");
            keywords.Add("group");
            keywords.Add("like");
            keywords.Add("top");

            this.codeTextArea.Text = "Select 'Ciao', * from tblProducts\nSelect 'Ciao', * from tblProducts\nSelect 'Ciao', * from tblProducts\n";
        }

        private void codeTextArea_CodeChanged(object sender, CodeTextAreaCodeChangedEventArgs e)
        {
            this.codeTextArea.Lines.Clear();

            string[] strLines = this.codeTextArea.Text.Split('\n');

            Regex re = new Regex(@"\w+|(?<q>[']).*?\k<q>|,|\*|\s+|\n", RegexOptions.Compiled);

            foreach (string strLine in strLines)
            {
                CodeTextAreaLine objLine = new CodeTextAreaLine();

                MatchCollection matches = re.Matches(strLine);

                foreach (Match match in matches)
                {
                    CodeTextAreaWord word = new CodeTextAreaWord();
                    word.Text = match.Value;

                    if (keywords.Contains(word.Text.ToLower()))
                    {
                        word.TextColor = Color.Blue;
                        word.TextFont = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic);
                    }
                    else
                    {
                        word.TextColor = Color.Gray;
                        word.TextFont = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Regular);
                    }

                    objLine.Words.Add(word);
                }

                this.codeTextArea.Lines.Add(objLine);
            }
        }
    }
}